package com.wzq.app.ui;

import android.content.Intent;
import android.os.Bundle;
import android.util.Log;
import android.view.View;
import android.widget.AdapterView;
import android.widget.ListView;
import android.widget.TextView;

import com.wzq.app.R;
import com.wzq.app.adapter.EvaAdapter;
import com.wzq.app.api.Api;
import com.wzq.app.api.ApiResponseCode;
import com.wzq.app.base.BaseActivity;
import com.wzq.app.constant.App;
import com.wzq.app.manager.UserManager;
import com.wzq.app.model.BaseModel;
import com.wzq.app.model.EvaModel;
import com.wzq.app.utils.ToastUtils;

import java.util.List;

import butterknife.BindView;
import butterknife.ButterKnife;
import retrofit2.Call;
import retrofit2.Callback;
import retrofit2.Response;

/**
 * @ProjectName: 2021WZQ
 * @Package: com.wzq.app.ui
 * @ClassName: MyEvaActivity
 * @Description: java类作用描述
 * @UpdateRemark: 更新说明：
 * @Version: 1.0
 */
public class MyEvaActivity extends BaseActivity {
    private static final String TAG = "MyEvaActivity";

    @BindView(R.id.head_left)
    TextView headLeft;

    List<EvaModel> list;
    @BindView(R.id.listview)
    ListView listview;
    EvaAdapter evaAdapter;

    @Override
    public int getLayoutId() {
        return R.layout.myeva_activity;
    }

    @Override
    public void configView() {
        headLeft.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                finish();
            }
        });
    }

    private void initViews(){
        evaAdapter = new EvaAdapter(list, mContext);
        listview.setAdapter(evaAdapter);
        listview.setOnItemClickListener(new AdapterView.OnItemClickListener() {
            @Override
            public void onItemClick(AdapterView<?> adapterView, View view, int i, long l) {
                Intent intent = new Intent(MyEvaActivity.this,EvaDetailActivity.class);
                Bundle bundle = new Bundle();
                bundle.putSerializable("eva",list.get(i));
                intent.putExtras(bundle);
                startActivity(intent);
            }
        });
    }
    @Override
    public void requestData() {
        String type = UserManager.getInstance().getUser().getType();
        if (type.equals(App.STUDENT)) {
            getStu();
        }
        if (type.equals(App.TEACHER)) {
            getTec();
        }
    }

    /**
     * 获取学生评价
     */
    public void getStu() {
        Call<BaseModel<List<EvaModel>>> call = Api.getInstance().service.getStuEva(UserManager.getInstance().getUser().getId());
        call.enqueue(new Callback<BaseModel<List<EvaModel>>>() {
            @Override
            public void onResponse(Call<BaseModel<List<EvaModel>>> call, Response<BaseModel<List<EvaModel>>> response) {
                if (response.isSuccessful()) {
                    Log.d(TAG, response.toString());
                    BaseModel<List<EvaModel>> model = response.body();
                    if (model.code == ApiResponseCode.RESPHONE_OK) {
                        list = model.obj;
                        initViews();
                    } else {
                        ToastUtils.showToast(mContext, "获取学生评价失败");
                    }
                }
            }

            @Override
            public void onFailure(Call<BaseModel<List<EvaModel>>> call, Throwable t) {
                ToastUtils.showToast(mContext, "获取学生评价失败");
            }
        });
    }

    /**
     * 获取老师评价
     */
    public void getTec() {
        Call<BaseModel<List<EvaModel>>> call = Api.getInstance().service.getTeaEva(UserManager.getInstance().getUser().getId());
        call.enqueue(new Callback<BaseModel<List<EvaModel>>>() {
            @Override
            public void onResponse(Call<BaseModel<List<EvaModel>>> call, Response<BaseModel<List<EvaModel>>> response) {
                if (response.isSuccessful()) {
                    Log.d(TAG, response.toString());
                    BaseModel<List<EvaModel>> model = response.body();
                    if (model.code == ApiResponseCode.RESPHONE_OK) {
                        list = model.obj;
                        initViews();
                    } else {
                        ToastUtils.showToast(mContext, "获取老师评价失败");
                    }
                }
            }

            @Override
            public void onFailure(Call<BaseModel<List<EvaModel>>> call, Throwable t) {
                ToastUtils.showToast(mContext, "获取老师评价失败");
            }
        });
    }

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        // TODO: add setContentView(...) invocation
        ButterKnife.bind(this);
    }
}
